package com.ncepu.clubmanagement.controller;

import com.ncepu.clubmanagement.pojo.Result;
import com.ncepu.clubmanagement.service.PersonService;
import com.ncepu.clubmanagement.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private PersonService personService;

    @Autowired
    private JwtUtils jwtUtil;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/headImage/self")
    public Result uploadHeadImage( MultipartFile image) throws Exception {
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        String account = (String) claims.get("account");
        String url_new = personService.updateHeadImage(account, image);
        if (url_new == null){
            return Result.error("上传失败");
        }
        log.info("用户{}上传头像至远端成功,相应url为{}",account,url_new);
        return Result.success(url_new);
    }

}
